package com.xuerhua.yxt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.dao.SchoolPropertyGoodsAttrMapper;
import com.xuerhua.yxt.dao.SchoolPropertyGoodsNameMapper;
import com.xuerhua.yxt.dao.SchoolPropertyGoodsStockDetailMapper;
import com.xuerhua.yxt.dao.SchoolPropertyGoodsStockMapper;
import com.xuerhua.yxt.dao.SchoolPropertyGoodsTypeMapper;
import com.xuerhua.yxt.dao.SchoolPropertyInputDetailMapper;
import com.xuerhua.yxt.dao.SchoolPropertyInputMapper;
import com.xuerhua.yxt.dao.SchoolPropertyOutputDetailMapper;
import com.xuerhua.yxt.dao.SchoolPropertyOutputRecordMapper;
import com.xuerhua.yxt.model.SchoolPropertyGoodsAttr;
import com.xuerhua.yxt.model.SchoolPropertyGoodsName;
import com.xuerhua.yxt.model.SchoolPropertyGoodsStock;
import com.xuerhua.yxt.model.SchoolPropertyGoodsStockDetail;
import com.xuerhua.yxt.model.SchoolPropertyGoodsType;
import com.xuerhua.yxt.model.SchoolPropertyInput;
import com.xuerhua.yxt.model.SchoolPropertyInputDetail;
import com.xuerhua.yxt.model.SchoolPropertyOutputDetail;
import com.xuerhua.yxt.model.SchoolPropertyOutputRecord;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.SchoolPropertyExportQueryVo;
import com.xuerhua.yxt.reqVo.SchoolPropertyGoodTypeReqVo;
import com.xuerhua.yxt.reqVo.SchoolPropertyGoodsStockDetailQueryVo;
import com.xuerhua.yxt.reqVo.SchoolPropertyInputReqVo;
import com.xuerhua.yxt.reqVo.SchoolPropertyOutputRecordReqVo;
import com.xuerhua.yxt.service.ISchoolPropertyService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.DateUtil;
import com.xuerhua.yxt.view.SchoolPropertyConsumablesApplyExportView;
import com.xuerhua.yxt.view.SchoolPropertyGoodsNameView;
import com.xuerhua.yxt.view.SchoolPropertyGoodsTypeView;
import com.xuerhua.yxt.view.SchoolPropertyInputExportView;
import com.xuerhua.yxt.view.SchoolPropertyLendAndReturnExportView;
import com.xuerhua.yxt.view.SchoolPropertyOutputRecordDetailView;
import com.xuerhua.yxt.view.SchoolPropertyOutputRecordView;
import com.xuerhua.yxt.view.SchoolPropertyStockExportView;
import com.xuerhua.yxt.view.SchoolPropertyStockView;

@Service("schoolPropertyService")
public class SchoolPropertyServiceImpl implements ISchoolPropertyService {

    @Autowired
    private SchoolPropertyInputMapper schoolPropertyInputMapper;
    @Autowired
    private SchoolPropertyInputDetailMapper schoolPropertyInputDetailMapper;
    @Autowired
    private SchoolPropertyGoodsTypeMapper schoolPropertyGoodsTypeMapper;
    @Autowired
    private SchoolPropertyGoodsNameMapper schoolPropertyGoodsNameMapper;
    @Autowired
    private SchoolPropertyGoodsAttrMapper schoolPropertyGoodsAttrMapper;
    @Autowired
    private SchoolPropertyGoodsStockMapper schoolPropertyGoodsStockMapper;
    @Autowired
    private SchoolPropertyGoodsStockDetailMapper schoolPropertyGoodsStockDetailMapper;
    @Autowired
    private SchoolPropertyOutputRecordMapper schoolPropertyOutputRecordMapper;
    @Autowired
    private SchoolPropertyOutputDetailMapper schoolPropertyOutputDetailMapper;


    // 添加入库信息
    @Override
    public int insertStock(SchoolPropertyInputReqVo vo) throws Exception {

        //保存物品名称或属性
        if (vo.getGoodsNameId() == null || vo.getGoodsAttrId() == null)
            saveGoodsNameAndGoodsAttr(vo);

        Integer inputId = vo.getInputId();

        // 保存入库单号信息
        if (inputId == null)
            inputId = saveInputMessage(vo);

        // 添加入库单号的物品详情
        saveInputGoodsDetail(vo, inputId);
        // 更新总金额
        schoolPropertyInputMapper.updateTotalPriceById(inputId, vo.getNumber() * vo.getPrice());
        // 更新库存信息
        updateStock(vo);
        return inputId;
    }

    //在入库时，如果有新添加的物品以及属性，就先在数据库添加记录
    private void saveGoodsNameAndGoodsAttr(SchoolPropertyInputReqVo vo) {
        SchoolPropertyGoodsName goodsName = new SchoolPropertyGoodsName();
        //先保存物品名称到物品名称表中
        if (vo.getGoodsNameId() == null) {
            goodsName.setSchoolId(vo.getSchoolId());
            goodsName.setTypeId(vo.getTypeId());
            goodsName.setTypeName(vo.getTypeName());
            goodsName.setGoodsName(vo.getGoodsName());
            schoolPropertyGoodsNameMapper.insertSelective(goodsName);
            vo.setGoodsNameId(goodsName.getId());
        }
        //再保存物品属性
        if (vo.getGoodsAttrId() == null) {
            SchoolPropertyGoodsAttr goodsAttr = new SchoolPropertyGoodsAttr();
            goodsAttr.setGoodsId(vo.getGoodsNameId());
            goodsAttr.setSchoolId(vo.getSchoolId());
            goodsAttr.setGoogsAttr(vo.getGoodsAttr());
            schoolPropertyGoodsAttrMapper.insertSelective(goodsAttr);
            vo.setGoodsAttrId(goodsAttr.getId());
        }
    }

    @Override
    public int insertGoodsType(SchoolPropertyGoodTypeReqVo record) {
        SchoolPropertyGoodsType copyAs = BeanUtils.copyAs(record, SchoolPropertyGoodsType.class);
        copyAs.setCreateTieme(new Date());
        return schoolPropertyGoodsTypeMapper.insertSelective(copyAs);
    }

    @Override
    public List<SchoolPropertyGoodsType> selectTypeByPage(Page<SchoolPropertyGoodsType> page) {
        return schoolPropertyGoodsTypeMapper.selectTypeByPage(page);
    }

    @Override
    public List<SchoolPropertyGoodsName> selectGoodsName(Integer typeId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<SchoolPropertyInput> selectInputRecordByPage(Page<SchoolPropertyInput> page) {
        return schoolPropertyInputMapper.selectInputRecordByPage(page);
    }

    @Override
    public SchoolPropertyGoodsType insertType(SchoolPropertyGoodsType type) {
        schoolPropertyGoodsTypeMapper.insertSelective(type);
        return type;
    }

    @Override
    public SchoolPropertyGoodsName insertName(SchoolPropertyGoodsName name) {
        schoolPropertyGoodsNameMapper.insertSelective(name);
        return name;
    }

    @Override
    public SchoolPropertyGoodsAttr insertAttr(SchoolPropertyGoodsAttr attr) {
        schoolPropertyGoodsAttrMapper.insertSelective(attr);
        return attr;
    }

    //查询物品类别
    @Override
    public List<SchoolPropertyGoodsTypeView> selectAllBySchoolId(Integer schoolId, Integer cycleUse) {
        // 查询
        SchoolPropertyGoodsType param1 = new SchoolPropertyGoodsType();
        param1.setSchoolId(schoolId);
        param1.setCycleUse(cycleUse);
        List<SchoolPropertyGoodsTypeView> types = schoolPropertyGoodsTypeMapper.selectTypeViewByParams(param1);

        SchoolPropertyGoodsName param2 = new SchoolPropertyGoodsName();
        param2.setSchoolId(schoolId);
        List<SchoolPropertyGoodsNameView> names = schoolPropertyGoodsNameMapper.selectGoodsNameViewByParams(param2);

        types.parallelStream().forEach(e -> mapNameToType(types, e, names));

        return types;
    }

    private void mapNameToType(List<SchoolPropertyGoodsTypeView> types, SchoolPropertyGoodsTypeView type, List<SchoolPropertyGoodsNameView> names) {
        if (type.getId() != null) {
            List<SchoolPropertyGoodsNameView> collect = names.parallelStream()
                    .filter(e -> Objects.equals(e.getTypeId(), type.getId())).collect(Collectors.toList());
            type.setNames(collect);
        }
    }

    /* 保存入库单号信息 */
    private int saveInputMessage(SchoolPropertyInputReqVo vo) {
        StringBuffer buff = new StringBuffer("S").append(vo.getSchoolId()).append("T")
                .append(DateUtil.getStrFromDate(DateUtil.DEFAULT_DATE_FORMAT4, new Date())).append("U").append(vo.getOperatorId());

        SchoolPropertyInput input = new SchoolPropertyInput();
        input.setNo(buff.toString());
        input.setCreateTime(new Date());
        input.setOperatorId(vo.getOperatorId());
        input.setOperatorName(vo.getOperatorName());
        input.setSchoolId(vo.getSchoolId());
        schoolPropertyInputMapper.insertSelective(input);
        return input.getId();
    }

    /* 添加入库单号的物品详情 */
    private void saveInputGoodsDetail(SchoolPropertyInputReqVo vo, Integer inputId) {
        SchoolPropertyInputDetail detail = new SchoolPropertyInputDetail();
        detail.setDescribe(vo.getDescribe());
        detail.setTypeName(vo.getTypeName());
        detail.setGoodsAttr(vo.getGoodsAttr());
        detail.setGoodsName(vo.getGoodsName());
        detail.setTypeId(vo.getTypeId());
        detail.setGoodsAttrId(vo.getGoodsAttrId());
        detail.setGoodsNameId(vo.getGoodsNameId());
        detail.setNumber(vo.getNumber());
        detail.setPrice(vo.getPrice());
        detail.setInputId(inputId);
        detail.setBuyTime(vo.getBuyTime());
        detail.setPlace(vo.getPlaceSite());
        detail.setUnits(vo.getUnits());
        detail.setSchoolId(vo.getSchoolId());
        schoolPropertyInputDetailMapper.insertSelective(detail);
    }

    /* 修改库存信息 */
    private void updateStock(SchoolPropertyInputReqVo s) throws Exception {

        SchoolPropertyGoodsStock params = new SchoolPropertyGoodsStock();
        params.setTypeId(s.getTypeId());
        params.setGoodsAttrId(s.getGoodsAttrId());
        params.setGoodsNameId(s.getGoodsNameId());
        params.setSchoolId(s.getSchoolId());

        //非消耗品编号
        int no = 0;
        //通参数查询并修改库存更改版本号
        int flag = schoolPropertyGoodsStockMapper.updateVersionForModify(params);
        //查询出库存信息
        SchoolPropertyGoodsStock stock = schoolPropertyGoodsStockMapper.selectStockByParams(params);
        if (flag == 0 && stock != null)
            throw new Exception("其他用户正在修改库存信息");
        // 如果还没有记录，则添加
        if (stock == null) {
            stock = new SchoolPropertyGoodsStock();
            stock.setTypeId(s.getTypeId());
            stock.setGoodsAttrId(s.getGoodsAttrId());
            stock.setGoodsNameId(s.getGoodsNameId());
            stock.setTypeName(s.getTypeName());
            stock.setGoodsName(s.getGoodsName());
            stock.setGoodsAttr(s.getGoodsAttr());
            stock.setSchoolId(s.getSchoolId());
            stock.setTotalStock(s.getNumber());
            stock.setCurrentStock(s.getNumber());
            stock.setCycleUse(s.getCycleUse());
            schoolPropertyGoodsStockMapper.insertSelective(stock);
            no = 0;
        } else {
            no = stock.getTotalStock();
            stock.setTotalStock(s.getNumber() + stock.getTotalStock());
            stock.setCurrentStock(s.getNumber() + stock.getCurrentStock());

            schoolPropertyGoodsStockMapper.updateByPrimaryKeySelective(stock);
        }
        addStockDetail(s, stock, no);
        schoolPropertyGoodsStockMapper.updateVersionForReset(params);
    }

    // 添加库存详情
    private void addStockDetail(SchoolPropertyInputReqVo s, SchoolPropertyGoodsStock stock, int no) {
        // 如果是非消耗品,则添加库存物品详情
        if (s.getCycleUse() == 1) {
            SchoolPropertyGoodsStockDetail detail = new SchoolPropertyGoodsStockDetail();

            detail.setDescribe(s.getDescribe());
            detail.setStockId(stock.getId());
            detail.setCreateTime(new Date());
            detail.setSchoolId(s.getSchoolId());
            for (int i = 0; i < s.getNumber(); i++) {
                detail.setCurrentNo(no + i + 1);
                StringBuffer buff = new StringBuffer("T").append(s.getTypeId()).append(s.getGoodsNameId())
                        .append(s.getGoodsAttrId()).append("-").append(detail.getCurrentNo());
                detail.setGoodsNo(buff.toString());
                schoolPropertyGoodsStockDetailMapper.insertSelective(detail);
            }
        }
    }

    //查询库存信息
    @Override
    public List<SchoolPropertyStockView> selectStockByPage(Page<SchoolPropertyStockView> page) {

        return schoolPropertyGoodsStockMapper.selectStockByPage(page);
    }

    //添加出库记录
    @Override
    public void insertOutputRecord(SchoolPropertyOutputRecordReqVo reqvo) throws Exception {

        SchoolPropertyGoodsStock params = new SchoolPropertyGoodsStock();
        params.setTypeId(reqvo.getTypeId());
        params.setGoodsAttrId(reqvo.getGoodsAttrId());
        params.setGoodsNameId(reqvo.getGoodsNameId());
        params.setSchoolId(reqvo.getSchoolId());
        //修改version
        schoolPropertyGoodsStockMapper.updateVersionForModify(params);
        SchoolPropertyGoodsStock stock = schoolPropertyGoodsStockMapper.selectStockByParams(params);

        SchoolPropertyOutputRecord record = BeanUtils.copyAs(reqvo, SchoolPropertyOutputRecord.class);
        record.setStockId(stock.getId());
        record.setState(0); //0：借出

        if (stock.getTotalStock() < reqvo.getNumber())
            throw new Exception("库存不足");
        //消耗品
        if (reqvo.getCycleUse() == 0) {
            //更新库存
            stock.setCurrentStock(stock.getCurrentStock() - reqvo.getNumber());
            stock.setTotalStock(stock.getTotalStock() - reqvo.getNumber());
            schoolPropertyGoodsStockMapper.updateByPrimaryKeySelective(stock);
            //添加记录
            schoolPropertyOutputRecordMapper.insertSelective(record);
        } else {
            //更新库存
            stock.setCurrentStock(stock.getCurrentStock() - reqvo.getNumber());
            schoolPropertyGoodsStockMapper.updateByPrimaryKeySelective(stock);
            schoolPropertyOutputRecordMapper.insertSelective(record);

            String detailIds = reqvo.getDetailIds();

            if (detailIds == null || detailIds.trim().length() < 1)
                throw new Exception("没有选择物品编号");

            //保存选中的物品编号Id
            String[] split = detailIds.split(",");
            SchoolPropertyGoodsStockDetail stockDetail = new SchoolPropertyGoodsStockDetail();
            SchoolPropertyOutputDetail outputDetail = new SchoolPropertyOutputDetail();
            stockDetail.setUsedId(reqvo.getUsedId());
            stockDetail.setUsedName(reqvo.getUsedName());
            stockDetail.setUsedPlace(reqvo.getUsedPlace());
            stockDetail.setGoodsState(1);
            stockDetail.setDescribe(reqvo.getDescribe());
            stockDetail.setOutputId(record.getId());
            stockDetail.setSchoolId(reqvo.getSchoolId());
            outputDetail.setOutputId(record.getId());

            for (int i = 0; i < split.length; i++) {
                if (split[i].trim().length() > 0) {
                    Integer temp = Integer.parseInt(split[i].trim());
                    stockDetail.setId(temp);
                    outputDetail.setGoodsId(temp);
                    outputDetail.setSchoolId(reqvo.getSchoolId());
                    schoolPropertyGoodsStockDetailMapper.updateByPrimaryKeySelective(stockDetail);
                    schoolPropertyOutputDetailMapper.insertSelective(outputDetail);
                }
            }
        }
        schoolPropertyGoodsStockMapper.updateVersionForReset(params);
    }

    @Override
    public List<SchoolPropertyOutputRecord> seleOutputRecordByPage(Page<SchoolPropertyOutputRecord> page) {
        return schoolPropertyOutputRecordMapper.selectRecordByPage(page);
    }

    @Override
    public List<SchoolPropertyGoodsStockDetail> selectStockDetailByPage(Page<SchoolPropertyGoodsStockDetail> page) {
        return schoolPropertyGoodsStockDetailMapper.selectStockDetailByPage(page);
    }


    @Override
    public List<SchoolPropertyInputDetail> selectInputDetailByPage(Page<SchoolPropertyInputDetail> page) {

        return schoolPropertyInputDetailMapper.selectInputDetailByPage(page);
    }

    @Override
    public SchoolPropertyOutputRecord selectOutputRecord(Integer outputId) {
        return schoolPropertyOutputRecordMapper.selectByPrimaryKey(outputId);
    }

    @Override
    public SchoolPropertyOutputRecordView selectOutputRecordView(Integer outputId) {

        return schoolPropertyOutputRecordMapper.selectOutputRecordView(outputId);
    }

    //确认归还
    @Override
    public void updateSchoolPropertyOutputRecord(SchoolPropertyOutputRecordReqVo vo) throws Exception {
        Date updateTime = new Date();
        SchoolPropertyOutputRecord record = schoolPropertyOutputRecordMapper.selectByPrimaryKey(vo.getId());
        if (record == null)
            throw new Exception("该条记录不存在");

        //修改版本号，更新库存
        SchoolPropertyGoodsStock params = new SchoolPropertyGoodsStock();
        params.setTypeId(record.getTypeId());
        params.setGoodsAttrId(record.getGoodsAttrId());
        params.setGoodsNameId(record.getGoodsNameId());
        params.setSchoolId(record.getSchoolId());
        int flag = schoolPropertyGoodsStockMapper.updateVersionForModify(params);

        if (flag == 0)
            throw new Exception("其他用户正在修改库存，修改失败");

        //损坏的物品Id
        List<Integer> ids = getFailGoodsId(vo.getDetailIds());
        //修改库存量
        SchoolPropertyGoodsStock stock = schoolPropertyGoodsStockMapper.selectStockByParams(params);
        if (stock != null)
            stock.setCurrentStock(stock.getCurrentStock() + (record.getNumber() - ids.size()));

        schoolPropertyGoodsStockMapper.updateByPrimaryKeySelective(stock);

        //将出库记录修改成已归还
        SchoolPropertyOutputRecord outputRecord = new SchoolPropertyOutputRecord();
        outputRecord.setUpdateTime(updateTime);
        outputRecord.setState(1);
        outputRecord.setFeedback(vo.getDescribe());
        outputRecord.setId(vo.getId());
        outputRecord.setSchoolId(vo.getSchoolId());
        schoolPropertyOutputRecordMapper.updateByPrimaryKeySelective(outputRecord);
        //使用出库记录ID,修改出库记录对应的物品状态为可用
        schoolPropertyGoodsStockDetailMapper.updateStateByOutputId(0, vo.getId(), vo.getSchoolId());
        //保存损坏的物品信息
        if (ids.size() > 0) {
            SchoolPropertyGoodsStockDetail failGoods = new SchoolPropertyGoodsStockDetail();
            failGoods.setUpdateTime(updateTime);
            failGoods.setGoodsState(2);
            failGoods.setSchoolId(vo.getSchoolId());
            ids.stream().forEach(id -> {
                failGoods.setId(id);
                schoolPropertyGoodsStockDetailMapper.updateByPrimaryKeySelective(failGoods);
            });
        }
        //修改版本号
        schoolPropertyGoodsStockMapper.updateVersionForReset(params);
    }

    private List<Integer> getFailGoodsId(String goodsIds) {

        if (goodsIds != null && !goodsIds.equals("")) {
            String[] split = goodsIds.split(",");
            if (split.length > 0) {
                List<Integer> ids = new ArrayList<>(split.length);
                for (String string : split) {
                    String trim = string.trim();
                    if (trim.length() > 0) {
                        Integer temp = Integer.parseInt(trim);
                        ids.add(temp);
                    }
                }
                return ids;
            }
        }
        return new ArrayList<>();
    }

    @Override
    public SchoolPropertyGoodsStock selectStockNumberByParams(SchoolPropertyGoodsStockDetailQueryVo qvo) {
        SchoolPropertyGoodsStock params = BeanUtils.copyAs(qvo, SchoolPropertyGoodsStock.class);
        return schoolPropertyGoodsStockMapper.selectStockByParams(params);
    }

    @Override
    public List<SchoolPropertyStockExportView> selectAllStockForExportToExcelByParams(SchoolPropertyExportQueryVo qvo) {
        SchoolPropertyGoodsStockDetailQueryVo vo = new SchoolPropertyGoodsStockDetailQueryVo();
        List<SchoolPropertyStockExportView> list = schoolPropertyGoodsStockMapper.selectAllStockForExportToExcelByParams(qvo);
        list.stream().filter(x -> x.getCycleUse() == 1).forEach(x -> {
            vo.setStockId(x.getId());
            List<SchoolPropertyGoodsStockDetail> list2 = schoolPropertyGoodsStockDetailMapper.selectStockDetailByParams(vo);
            StringBuffer buff = new StringBuffer();
            list2.stream().forEach(y -> {
                buff.append(",").append(y.getGoodsNo());
            });
            if (buff.length() > 1)
                x.setNos(buff.substring(1).toString());
        });
        return list;
    }

    @Override
    public List<SchoolPropertyStockExportView> selectAllStockForExportToExcelByPage(Page<SchoolPropertyStockExportView> page) {
        SchoolPropertyGoodsStockDetailQueryVo vo = new SchoolPropertyGoodsStockDetailQueryVo();
        List<SchoolPropertyStockExportView> list = schoolPropertyGoodsStockMapper.selectAllStockForExportToExcelByPage(page);

        //应该是对list进行过滤，然后对x循环
		//filter()：对符合表达式的每一个对象过滤
		//forEach()：循环每个filter过滤出的对象
        list.stream().filter(x -> x.getCycleUse() == 1).forEach(x -> {
            vo.setStockId(x.getId());
            vo.setSchoolId(x.getSchoolId());
            List<SchoolPropertyGoodsStockDetail> list2 = schoolPropertyGoodsStockDetailMapper.selectStockDetailByParams(vo);
            StringBuffer buff = new StringBuffer();
            list2.stream().forEach(y -> {
                buff.append(",").append(y.getGoodsNo());
            });
            if (buff.length() > 1)
                x.setNos(buff.substring(1).toString());
        });
        return list;
    }

    @Override
    public List<SchoolPropertyInputExportView> selectSchoolPropertyInputForExportToExcelByParams(
            SchoolPropertyExportQueryVo vo) {
        return schoolPropertyInputMapper.selectSchoolPropertyInputForExportToExcelByParams(vo);
    }

    @Override
    public List<SchoolPropertyInputExportView> selectSchoolPropertyInputForExportToExcelByPage(Page<SchoolPropertyInputExportView> page) {
        return schoolPropertyInputMapper.selectSchoolPropertyInputForExportToExcelByPage(page);
    }

    @Override
    public List<SchoolPropertyConsumablesApplyExportView> selectSchoolPropertyConsumablesApplyForExportToExcelByParams(
            SchoolPropertyExportQueryVo vo) {
        return schoolPropertyOutputRecordMapper.selectSchoolPropertyConsumablesApplyForExportToExcelByParams(vo);
    }

    @Override
    public List<SchoolPropertyConsumablesApplyExportView> selectSchoolPropertyConsumablesApplyForExportToExcelByPage(
            Page<SchoolPropertyConsumablesApplyExportView> page) {
        return schoolPropertyOutputRecordMapper.selectSchoolPropertyConsumablesApplyForExportToExcelByPage(page);
    }

    @Override
    public List<SchoolPropertyLendAndReturnExportView> selectSchoolPropertyLendAndReturnForExportToExcelByParams(
            SchoolPropertyExportQueryVo qvo) {
        List<SchoolPropertyLendAndReturnExportView> list = schoolPropertyOutputRecordMapper.selectSchoolPropertyLendAndReturnForExportToExcelByParams(qvo);

        list.stream().forEach(x -> {
            List<SchoolPropertyOutputRecordDetailView> detail = schoolPropertyOutputDetailMapper.selectoutputRecordDetail(x.getId());
            StringBuffer buff = new StringBuffer();
            detail.stream().forEach(y -> {
                buff.append(",").append(y.getGoodsNo());
            });
            if (buff.length() > 1)
                x.setNos(buff.substring(1).toString());
        });
        return list;
    }

    @Override
    public List<SchoolPropertyLendAndReturnExportView> selectSchoolPropertyLendAndReturnForExportToExcelByPage(
            Page<SchoolPropertyLendAndReturnExportView> page) {
        List<SchoolPropertyLendAndReturnExportView> list = schoolPropertyOutputRecordMapper.selectSchoolPropertyLendAndReturnForExportToExcelByPage(page);

        list.stream().forEach(x -> {
            List<SchoolPropertyOutputRecordDetailView> detail = schoolPropertyOutputDetailMapper.selectoutputRecordDetail(x.getId());
            StringBuffer buff = new StringBuffer();
            detail.stream().forEach(y -> {
                buff.append(",").append(y.getGoodsNo());
            });
            if (buff.length() > 1)
                x.setNos(buff.substring(1).toString());
        });
        return list;
    }
}